/*====================================================================================
                                                                     
		Financial Constraints and Propagation of Shocks in Production Networks
			    By Demir-Javorcik-Michalski-Ors
  
*====================================================================================*/	

clear all
set more off
log close _all
set matsize 5000

cd "\data\" // Add your directory here

/***************************************
/Construct country-level productivity shocks
****************************************/
use "GDPpc_WDI.dta",clear
keep if seriesname=="GDP per capita (constant 2010 US$)"
drop seriesname 

reshape wide d,i(countrycode) j(year)
gen growth=(d2012- d2010)/d2010

keep growth countrycode
rename countrycode country_code
sort country_code
save tempgdp.dta,replace


//Construct USD and producitivity shocks
use "ithalat.dta",clear

rename sırano ID
rename sırano_donem year
rename kýymet imports

keep if year==2010

drop if regimecode==4051 | regimecode==4100 | regimecode==4171 | regimecode==4123 | regimecode==4191 | regimecode==4251 | regimecode==5100 | regimecode==5123 | regimecode==5141 | regimecode==5171 | regimecode==5191 | regimecode==5200 | regimecode==5221 | regimecode==5223 | regimecode==5271 | regimecode==5291 | regimecode==5341 | regimecode==5351 

gen HS6=floor(HS12/1000000)
collapse (sum) imports,by(ID country_code HS6)
bys ID ,sort: egen totalimports=sum(imports)
gen import_share=imports/totalimports

sort ID
merge ID using temp1.dta
drop _merge

sort ID
merge ID using temp2.dta
drop _merge

sort country_code 
merge  country_code  using "tempgdp.dta"
drop _merge

sort country_code HS6
merge country_code HS6 using "usd.dta" // This dataset has to be transferred from TSI
drop _merge


replace totalimports=0 if totalimports==.
drop if wage0==0
gen FirmExposurePry=(1.5*totalimports/(wage0+DomPurch0+1.5*totalimports))*import_share*growth
gen FirmExposureUSD=(1.5*totalimports/(wage0+DomPurch0+1.5*totalimports))*import_share*usdshare

collapse (sum)   FirmExposurePry FirmExposureUSD,by(ID)

sort ID
save tempplacebo.dta,replace

/***************************************
/Construct estimation sample
****************************************/
use temp4.dta,clear
drop if year==2008

drop nace4 nace2 province_code

sort ID 
merge ID  using "temp3.dta"
keep if _merge==3
drop _merge

gen nace2=floor(nace4/100)
keep if nace2>9 & nace2<34

sort ID year
merge ID year using temp14.dta
keep if _merge==3
drop _merge

keep ID year ShrMatInp ImpInt empl DomPurch   Sales imports indegree_total 
fillin ID year
drop _f

for X in any  imports ImpInt: replace X=0 if X==.

gen indegree_total2010=indegree_total if year==2010
for X in any ShrMatInp ImpInt DomPurch   Sales imports indegree_total: gen X2011=X if year==2011
for X in any ShrMatInp ImpInt DomPurch   Sales imports indegree_total:bys ID,sort: egen meanX2011=mean(X2011)
for X in any ShrMatInp ImpInt DomPurch   Sales imports indegree_total: replace X2011=meanX2011
drop mean*

gen ChgSales=ln(Sales/Sales2011)
gen ChgImpInt=ImpInt-ImpInt2011
gen ChgShrMatInp=ShrMatInp-ShrMatInp2011
gen ChgDomSupp=ln(indegree_total/indegree_total2011)

sort ID 
merge ID  using "temp3.dta"
keep if _merge==3
drop _merge

sort ID 
merge ID  using "temp8.dta"
keep if _merge==3
drop _merge

sort ID 
merge ID  using "temp9.dta"
keep if _merge==3
drop _merge

sort ID 
merge ID  using "tempplacebo.dta"
drop if _merge==2
drop _merge
replace FirmExposurePry=0 if FirmExposurePry==.

bys year ,sort: egen p5=pctile(ChgSales),p(5)
bys year ,sort: egen p95=pctile(ChgSales),p(95)
keep if  ChgSales>p5 & ChgSales<p95

gen Chgtau=FirmExposure*(ln(1+0.06)-ln(1+0.03))
gen ChgtauP=FirmExposureP*(ln(1+0.06)-ln(1+0.03))
gen ChgtauS=Exposure_s*(ln(1+0.06)-ln(1+0.03))
gen ChgtauB=Exposure_b*(ln(1+0.06)-ln(1+0.03))
gen ChgtauS_HFC=Exposure_s_HFC*(ln(1+0.06)-ln(1+0.03))
gen ChgtauS_LFC=Exposure_s_LFC*(ln(1+0.06)-ln(1+0.03))
gen ChgtauB_HFC=Exposure_b_HFC*(ln(1+0.06)-ln(1+0.03))
gen ChgtauB_LFC=Exposure_b_LFC*(ln(1+0.06)-ln(1+0.03))
gen ChgtauS_Size=Exposure_s_Emp*(ln(1+0.06)-ln(1+0.03))
gen ChgtauB_Size=Exposure_b_Emp*(ln(1+0.06)-ln(1+0.03))


gen lnEmp0=ln(empl0)

 foreach var of varlist Chgtau  ChgtauS ChgtauB  ChgtauS_HFC ChgtauS_LFC ChgtauB_HFC ChgtauB_LFC {
 gen LowFinCost`var'=LowFinCost*`var'
}

 foreach var of varlist Chgtau  ChgtauS ChgtauB   {
 gen Emp0`var'=lnEmp0*`var'
}

local period="2009 2010 2012 2013 2014"

 foreach var of varlist Chgtau  ImpInt0  lnEmp0   {
for X in num `period': gen `var'DX=(year==X)*`var'
}

egen sr=group(nace4 province)
egen st=group(nace4 year)
egen rt=group(province year)

save temp.dta,replace

/***************************************
/Figure 2
****************************************/
use temp.dta,clear

 foreach var of varlist ChgSales ChgImpInt ChgShrMatInp ChgDomSupp {
 reghdfe `var'  ChgtauD* ImpInt0D* lnEmp0D*  ,absorb(sr st rt) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace
}

noi esttab ChgSales ChgImpInt ChgShrMatInp ChgDomSupp, compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)

/***************************************
/Table II.1
****************************************/ 

keep if year==2012

su FirmExposure Exposure_s Exposure_b LowFinCost,det
su FirmExposure Exposure_s Exposure_b LowFinCost if ImpInt0>0,det

/***************************************
/Table II.2
****************************************/ 

reghdfe FirmExposure ImpInt0 lnEmp0 LowFinCost,absorb(sr) cluster(sr)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe FirmExposure ImpInt0 lnEmp0 LowFinCost if ImpInt0>0,absorb(sr) cluster(sr)
estimates store col2
estadd local "FE" "sr" , replace


noi esttab col1 col2 ,  compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar  )
 

/***************************************
/Table 1 (upper panel)
****************************************/

 foreach var of varlist ChgSales ChgImpInt ChgShrMatInp ChgDomSupp {
 reghdfe `var'  Chgtau ImpInt0 lnEmp0  ,absorb(sr) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace

}

noi esttab ChgSales ChgImpInt ChgShrMatInp ChgDomSupp, compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)

/***************************************
/Table 1 (lower panel)
****************************************/

 foreach var of varlist ChgSales ChgImpInt ChgShrMatInp ChgDomSupp {
 reghdfe `var'  Chgtau ChgtauS ChgtauB ImpInt0 lnEmp0  ,absorb(sr) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace
}

noi esttab ChgSales ChgImpInt ChgShrMatInp ChgDomSupp, compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)

 
/***************************************
/Table 2
****************************************/
 
foreach var of varlist ChgSales ChgImpInt ChgShrMatInp ChgDomSupp {
 reghdfe `var'  LowFinCostChgtau LowFinCostChgtauS LowFinCostChgtauB Chgtau ChgtauS ChgtauB ImpInt0 lnEmp0 LowFinCost  ,absorb(sr) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace
}

noi esttab ChgSales ChgImpInt ChgShrMatInp ChgDomSupp, compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)

 
/***************************************
/Table 3 (upper panel)
****************************************/
 
foreach var of varlist ChgSales ChgImpInt ChgShrMatInp ChgDomSupp {
 reghdfe `var'  Chgtau ChgtauS_HFC ChgtauS_LFC ChgtauB_HFC ChgtauB_LFC ImpInt0 lnEmp0   ,absorb(sr) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace
}

noi esttab ChgSales ChgImpInt ChgShrMatInp ChgDomSupp, compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)
 
/***************************************
/Table 3 (lower panel)
****************************************/
 
foreach var of varlist ChgSales ChgImpInt ChgShrMatInp ChgDomSupp {
 reghdfe `var'  LowFinCostChgtau LowFinCostChgtauS_HFC LowFinCostChgtauS_LFC LowFinCostChgtauB_HFC LowFinCostChgtauB_LFC  Chgtau ChgtauS_HFC ChgtauS_LFC ChgtauB_HFC ChgtauB_LFC ImpInt0 lnEmp0 LowFinCost   ,absorb(sr) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace
}

noi esttab ChgSales ChgImpInt ChgShrMatInp ChgDomSupp, compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)

 
/***************************************
/Table II.4
****************************************/
 
 foreach var of varlist ChgSales ChgImpInt ChgShrMatInp ChgDomSupp {
 reghdfe `var'  ChgtauP ImpInt0 lnEmp0 if Chgtau==0  ,absorb(sr) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace
}

noi esttab ChgSales ChgImpInt ChgShrMatInp ChgDomSupp, compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)

foreach var of varlist ChgSales ChgImpInt ChgShrMatInp ChgDomSupp {
 reghdfe `var'  Chgtau ImpInt0 lnEmp0 FirmExposurePry ,absorb(sr) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace
}

noi esttab ChgSales ChgImpInt ChgShrMatInp ChgDomSupp, compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)

 
 foreach var of varlist ChgSales ChgImpInt ChgShrMatInp ChgDomSupp {
 reghdfe `var'  Chgtau ImpInt0 lnEmp0 FirmExposureUSD ,absorb(sr) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace
}

noi esttab ChgSales ChgImpInt ChgShrMatInp ChgDomSupp, compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)

 foreach var of varlist ChgSales ChgImpInt ChgShrMatInp ChgDomSupp {
 reghdfe `var'  Chgtau ImpInt0 lnEmp0 debt_ratio ,absorb(sr) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace
}

noi esttab ChgSales ChgImpInt ChgShrMatInp ChgDomSupp, compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)

/***************************************
/Table II.11
****************************************/

 foreach var of varlist ChgSales ChgImpInt ChgShrMatInp ChgDomSupp {
 reghdfe `var'  LowFinCostChgtau LowFinCostChgtauS LowFinCostChgtauB Chgtau ChgtauS ChgtauB ImpInt0 lnEmp0 LowFinCost Emp0Chgtau Emp0ChgtauS Emp0ChgtauB  ,absorb(sr) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace
}

noi esttab ChgSales ChgImpInt ChgShrMatInp ChgDomSupp, compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)


/***************************************
/Table II.12
****************************************/

 foreach var of varlist ChgSales ChgImpInt ChgShrMatInp ChgDomSupp {
 reghdfe `var'  Chgtau ChgtauS_HFC ChgtauS_LFC ChgtauB_HFC ChgtauB_LFC ChgtauS_Size ChgtauB_Size  ImpInt0 lnEmp0  ,absorb(sr) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace
}

noi esttab ChgSales ChgImpInt ChgShrMatInp ChgDomSupp, compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)

